package com.pro01.backend.controller;

import com.pro01.backend.pojo.Forum;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.service.ForumService;
import com.pro01.backend.utils.JwtUtil;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 用户查看论坛接口
 */
@RestController
@RequestMapping("/forums")
public class ForumController {

    @Autowired
    private ForumService forumService;

    /**
     * 用户查看所有公开板块
     */
    @GetMapping
    public Result<List<Forum>> getAllVisibleForums(HttpServletRequest request) {
//        String token = request.getHeader("Authorization");
//        Map<String, Object> claims = JwtUtil.parseToken(token);
//
//        if (claims == null) {
//            return Result.error("无效的 Token，请重新登录");
//        }

        try {
            Long userId = TokenUtil.getUserId(request);

            List<Forum> forums = forumService.getVisibleForums();
            return Result.success(forums);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询板块失败：" + e.getMessage());
        }
    }
}
